package com.helloandroid.uitween.single

import android.view.View
import com.helloandroid.uitween.UITween


open class RotateTo(val duration: Float, var dstDegress: Float) : UITween() {

    var firstExecute = false

    var px = 0f
    var cx = 0f
    var elapsedTime = 0f

    override fun initialize(node: View) {
        cx = node.rotation
        px = (dstDegress - node.rotation) / duration
        elapsedTime = 0f
    }

    override fun execute(dt: Float) {
        val node = target!!
        if (!firstExecute) {
            initialize(node)
            firstExecute = true
        }

        elapsedTime += dt
        if (elapsedTime >= duration) {
            node.rotation = dstDegress
            end = true
        } else {
            cx += px * dt
            node.rotation = cx
        }
    }

    override fun clone(): UITween {
        return RotateTo(duration,dstDegress)
    }
}

class RotateBy(duration: Float, val dxDegress: Float) : RotateTo(duration,0f) {
    override fun initialize(node: View) {
        cx = node.rotation
        dstDegress = dxDegress + cx
        px = dxDegress / duration
        elapsedTime = 0f
    }

    override fun clone(): UITween {
        return RotateBy(duration,dxDegress)
    }
}
